import { defineStore } from 'pinia'
import { getCodeInfo } from '@/api/base/Base'

export const useCodeStore = defineStore('code', {
  state() {
    return {
      codeInfo: new Map() as Map<string, CodeInfo>
    }
  },
  getters: {},
  actions: {
    async InitCodeInfo() {
      const { data } = await getCodeInfo()
      data.forEach((ele: CodeInfo) => {
        this.codeInfo.set(ele.code, ele)
      })
    },
    getCodesByDict(dict: string): Code[] {
      return this.codeInfo.get(dict)!.codes.map((ele) => {
        return { label: ele.name, value: ele.value }
      })
    },
    getName(dict: string, value: string): string {
      return this.codeInfo.get(dict)!.codes.find((ele) => ele.value === value)!.name
    }
  }
})
